package com.da.javatest.niuke.baodian;

import com.da.javatest.niuke.common.TreeNode;

/**
 * @author chenlida
 * @date 2020/10/6 13:07
 * https://www.nowcoder.com/practice/b6bbed48cd864cf09a34a6ca14a3976f?tpId=8&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C18Balance {
    public boolean isBalance(TreeNode root) {
        if (root == null) {
            return true;
        }
        if (Math.abs(maxDepth(root.left) - maxDepth(root.right)) > 1) {
            return false;
        }
        return isBalance(root.left) && isBalance(root.right);
    }

    public int maxDepth(TreeNode root) {
        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }
}
